import java.awt.Color;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * This Class contains a JTextPane which is used for displaying
 * texts in Tasky.
 * 
 * This Class contains different methods which controls how
 * texts should be displayed onto the JTextPane component
 * 
 * @author Keith Lim Yong Ming A0097846J
 *
 */

public class DisplayWindow implements TextWindowInterface {

	private static final int DEFAULT_FONTSIZE = 16;

	private JTextPane jTextf;
	private TextPaneWrapper customWrapper;

	private static final boolean BOOL_EDITABLE = false;
	
	private ColorGenerator cgen;

	private static final String LOG_INITIALIZE = "Initialized Display Window Logger";
	private static final String LOG_NAME = "DisplayWindowLog.log";
	
	private static final String KEYWORD_BLANK = "";
	
	private static final String NAME_DISPLAYSTYLE = "displayStyle";
	private static final String NAME_ADDSTYLE = "addStyle";
	private static final String NAME_EDITSTYLE = "editStyle";
	private static final String NAME_DELETESTYLE = "deleteStyle";
	private static final String NAME_DEFAULTSTYLE = "defaultStyle";
	private static final String NAME_UNDOSTYLE = "undoStyle";
	private static final String NAME_HISTORYSTYLE = "historyStyle";
	private static final String NAME_DONESTYLE = "doneStyle";
	private static final String NAME_BROADCASTSTYLE = "broadCastStyle";
	private static final String NAME_ERRORSTYLE = "errorStyle";
	private static final String NAME_WARNINGSTYLE = "warningStyle";
	
	private static final String COLOR_DARKGREEN = "DARK GREEN";
	private static final String COLOR_ORANGE = "ORANGE";
	private static final String COLOR_PURPLE = "PURPLE";
	private static final String COLOR_DARKRED = "DARK RED";
	
	private Logger displayWindowLogger;

	public DisplayWindow(BaseWindow sF) {
		loadLog();
		
		instantiateObjects();
		
		setTextBoxEditor();

		setTextBoxEditable();
	}

	private void setTextBoxEditor() {
		jTextf.setEditorKit(customWrapper);
	}

	private void instantiateObjects() {
		jTextf = new JTextPane();

		cgen = new ColorGenerator();

		customWrapper = new TextPaneWrapper();
	}

	public void setTextBoxEditable() {
		jTextf.setEditable(BOOL_EDITABLE);
	}

	private void loadLog() {
		try {
			displayWindowLogger = Logger.getLogger(LOG_NAME);
			FileHandler fh = null;
			fh = new FileHandler(LOG_NAME);
			displayWindowLogger.addHandler(fh);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			
			displayWindowLogger.info(LOG_INITIALIZE);

		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public JTextPane getTextWindow() {
		return jTextf;
	}

	public void addDisplayText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style displayStyle = jTextf.addStyle(NAME_DISPLAYSTYLE, null);
			StyleConstants.setForeground(displayStyle, Color.BLUE);
			StyleConstants.setFontSize(displayStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, displayStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		}
	}

	public void addAddText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style addStyle = jTextf.addStyle(NAME_ADDSTYLE, null);
			StyleConstants.setForeground(addStyle,
					cgen.generateColor(COLOR_DARKGREEN));
			StyleConstants.setFontSize(addStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, addStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addEditText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style editStyle = jTextf.addStyle(NAME_EDITSTYLE, null);
			StyleConstants.setForeground(editStyle,
					cgen.generateColor(COLOR_PURPLE));
			StyleConstants.setFontSize(editStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, editStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addDeleteText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style deleteStyle = jTextf.addStyle(NAME_DELETESTYLE, null);
			StyleConstants.setForeground(deleteStyle,
					cgen.generateColor(COLOR_DARKRED));
			StyleConstants.setFontSize(deleteStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, deleteStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addDefaultText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style normStyle = jTextf.addStyle(NAME_DEFAULTSTYLE, null);
			StyleConstants.setFontSize(normStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, normStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		}
	}

	public void addUndoText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style undoStyle = jTextf.addStyle(NAME_UNDOSTYLE, null);
			StyleConstants.setForeground(undoStyle, cgen
					.generateColor(COLOR_ORANGE).darker());
			StyleConstants.setFontSize(undoStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, undoStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addHistoryText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style historyStyle = jTextf.addStyle(NAME_HISTORYSTYLE, null);
			StyleConstants.setForeground(historyStyle, Color.magenta.darker());
			StyleConstants.setFontSize(historyStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, historyStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addDoneText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style historyStyle = jTextf.addStyle(NAME_DONESTYLE, null);
			StyleConstants.setForeground(historyStyle, Color.PINK.darker());
			StyleConstants.setFontSize(historyStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), newMsg, historyStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addNewAnnoucement(String announceThis) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();

			Style bStyle = jTextf.addStyle(NAME_BROADCASTSTYLE, null);
			StyleConstants.setForeground(bStyle, Color.DARK_GRAY);
			StyleConstants.setBold(bStyle, true);
			StyleConstants.setFontSize(bStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), announceThis, bStyle);

		} catch (BadLocationException exc) {
			exc.printStackTrace();
		}
	}

	public void addErrorMsg(String errMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();

			Style errStyle = jTextf.addStyle(NAME_ERRORSTYLE, null);
			StyleConstants.setForeground(errStyle, Color.red);
			StyleConstants.setFontSize(errStyle, DEFAULT_FONTSIZE);

			doc.insertString(doc.getLength(), errMsg, errStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		}
	}
	
	public void addWarningText(String newMsg) {
		try {
			StyledDocument doc = jTextf.getStyledDocument();
			Style warnStyle = jTextf.addStyle(NAME_WARNINGSTYLE, null);
			StyleConstants.setForeground(warnStyle,Color.orange);
			StyleConstants.setFontSize(warnStyle, DEFAULT_FONTSIZE);
			StyleConstants.setItalic(warnStyle, true);

			doc.insertString(doc.getLength(), newMsg, warnStyle);
		} catch (BadLocationException exc) {
			exc.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public void clearAllMsg() {
		jTextf.setText(KEYWORD_BLANK);
	}
}
